window.onload = function () {
    initTableHead();
    initTableBody();
}


var columns = [
    {
        isNO: true,
        title: '编号',
    }, {
        field: 'dept',
        title: '部门',
    }, {
        field: 'title',
        title: '职务',
    }, {
        field: 'name',
        title: '姓名',
    }
]

function initTableHead() {

    var tbl = document.getElementById('list');
    var thead = document.createElement('thead');
    var tRow = document.createElement('tr');

    tbl.appendChild(thead);
    thead.appendChild(tRow);

    for (var key in columns) {
        var theader = document.createElement('th');
        theader.innerText = columns[key].title;
        tRow.appendChild(theader);
    }

    /*  var theader1 = document.createElement('th');
      theader1.innerText = '编号';

      var theader2 = document.createElement('th');
      theader2.innerText = '姓名';

      tRow.appendChild(theader1);
      tRow.appendChild(theader2);*/

}


function initTableBody() {

    var tbl = document.getElementById('list');
    var tableBody = document.createElement('tbody');
    tbl.appendChild(tableBody);

    for (var key in employees) {
        var tRow = document.createElement('tr');
        tableBody.appendChild(tRow);
        // setTds1(key, tRow);

        /*对象循环*/
        /*var emp = {
             name: 'L',
             gender:'male'
        }

        for (var key in emp){
            alert(emp[key]);
        }*/


        for (var innerK in columns) {

            var tData = document.createElement('td');
            if(employees[key][columns[innerK].field])

            {
                tData.innerText = employees[key][columns[innerK].field];

            }else{
                tData.innerText = parseInt(key+1);
            }

            tRow.appendChild(tData);
        }

        /*   for (var innerK in employees[key]) {

               /!*     alert(innerK);
                    alert(employees[key][innerK])*!/

               var tData = document.createElement('td');
               tData.innerText = employees[key][innerK];
               tRow.appendChild(tData);
           }*/

    }

}


function setTds1(key, tRow) {
    var tdIndex = document.createElement('td');
    tdIndex.innerText = parseInt(key + 1);

    var tdName = document.createElement('td');
    tdName.innerText = employees[key].name;

    tRow.appendChild(tdIndex);
    tRow.appendChild(tdName);
    return {tdIndex, tdName};
}